//--------------------------------------
// -- 适用: 所有页面 --
//--------------------------------------

//在IE下，event对象有srcElement属性，但是没有target属性；Firefox下，even对象有target属性，但是没有srcElement属性.
//getEvent()函数可以兼容firefox和ie,只需要在访问事件对象的函数的开始调用getEvent()即可
function getEvent() {
    if (document.all) {
        return window.event; //如果是ie
    }
    func = getEvent.caller;
    while (func != null) {
        var arg0 = func.arguments[0];
        if (arg0) {
            if ((arg0.constructor == Event || arg0.constructor == MouseEvent)
|| (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
                return arg0;
            }
        }
        func = func.caller;
    }
    return null;
}

// 载入页面后相关操作
function initpage() {
	if (parent.location != self.location) {
		if (parent.document.all("divEmLeft") != undefined && parent.document.all("divEmLeft").innerText == "显示") {
			document.all("mainleft").style.display = "none";
		}
	}
}
//--------------------------------------
// 显示隐藏左边消息列表
function showLeftList(n) {
//	var LeftListStyle = document.all("divLeftList"+n).style;
//	if (LeftListStyle.display == "none") {
//		LeftListStyle.display = "";
//		document.images("imgLeftList"+n).src = "../images/ico_listup.gif";
//	} else {
//		LeftListStyle.display = "none";
//		document.images("imgLeftList"+n).src = "../images/ico_listdown.gif";
//}
var LeftListStyle = $('#divLeftList' + n);
if (LeftListStyle.css('display') == "none") {
    LeftListStyle.css('display','block')
    $("#imgLeftList" + n).attr('src', "../images/ico_listup.gif");
} else {
    LeftListStyle.css('display', 'none')
    $("#imgLeftList" + n).attr('src', "../images/ico_listdown.gif");
}

}
//--------------------------------------
// 显示隐藏右边内容列表
function showContentList(n) {
    var ContentListStyle = $("#divContentList" + n);
    if (ContentListStyle.css('display') == "none") {
        ContentListStyle.fadeIn();
		$("#imgContentList" + n).attr('src', '../images/ico_listup.gif');
	} else {
	    ContentListStyle.fadeOut();
	    $("#imgContentList" + n).attr('src', '../images/ico_listdown.gif');
	}
}
//--------------------------------------
// 打开对话框
function showDialog(urlstr, width, height) {
	var xml=showModalDialog(urlstr, "", "dialogWidth:"+width+"px; dialogHeight:"+height+"px; status:0");
	if (xml == undefined)
		return false;
	alert(xml);
}
function showtb(id1,id2) {
	var ids1 = document.all("th").value;
	var ids2 = document.all("td").value;
	if (ids1!="") {
		document.all(ids1).background="../images/lbx.gif";
		document.all(ids2).style.display="none";
	}
	document.all("th").value=id1;
	document.all("td").value=id2;
	$('#'+ id1).css('background',"../images/tab_bg.png");
	document.all(id2).style.display="";
}

// 列表选择: n=1全选, n=1反选, n=2全不选
function selectAllRows(tableName, n) {
    var table = $('#' + tableName);
    if (table.length > 0) {
        var objTb = table.find("input[name='dtRowID']");
        if (objTb.length <= 0) {
            return;
        }
        else {
            dtCount = objTb.length;
            switch (n) {
                case 0:
                    for (i = 1; i <= dtCount; i++) {
                        objTb[i-1].checked = false;
                    }
                    break;
                case 1:
                    for (i = 1; i <= dtCount; i++) {
                        objTb[i - 1].checked = true;
                    }
                    break;
                case 2:
                    for (i = 1; i <= dtCount; i++) {
                        if (objTb[i - 1].checked)
                            objTb[i - 1].checked = false;
                        else
                            objTb[i - 1].checked = true;
                    }
                    break;
            }
        }
    }
}
//--------------------------------------
// 取得当前对象所在编辑行号, 以0为起点
function getRowID(eleobj) {
    var objTr = eleobj.offsetParent.parentElement;
    var objTb = eleobj.offsetParent;
    while (objTb.tagName.toUpperCase() != "TABLE") {
        objTb = objTb.offsetParent;
    }
    var trCount = objTb.rows.length;
    if (objTb.all("dtRowID") == null)
        return 0;
    var dtCount = objTb.all("dtRowID").length;
    if (dtCount == undefined)
        dtCount = 1;
    for (i = 0; i < trCount; i++) {
        if (objTr == objTb.rows(i)) {
            return i + dtCount - trCount;
        }
    }
    return 0;
}
//--------------------------------------
// 取得空行模板Html串, 用于新建行
function getBlankRowElement(tableName) {
    var objTb = $('#' + tableName);
    var tbString = objTb.get(0).outerHTML;
    var trString = tbString.substring(0, tbString.indexOf("//HiddenTR//-->"));
    trString = trString.substring(trString.indexOf("<!--//HiddenTR//") + 16, trString.length);
    return trString;
}
//--------------------------------------
// 新建指定行
function insertNewRow(tableName) {
    var objTb = $('#' + tableName);
    if (objTb.length > 0) {
        var trString = getBlankRowElement(tableName);

        var objTbody = objTb.children("TBODY");
        if (objTbody.length > 0) {
            objTbody.append(trString);
            var objRowID = objTb.find("input[name='dtRowID']");
            updateRowNo(tableName);
        }
    }
}
//--------------------------------------
// 删除所有行
function deleteAllRows(tableName) {
    var table = $('#' + tableName);
    if (table.length > 0) {
        var objTb = table.find("input[name='dtRowID']");
        if (objTb.length <= 0) {
            return false;
        }
        else {
            objTb.each(function(i) {
                //input > tr > td
                $(this).parent().parent().remove();
            });
        }
    }
}
//--------------------------------------
// 删除指定行
function deleteSelectedRows(tableName) {
    var objTb = $('#' + tableName);
    if (objTb.length > 0) {
        var objRowID = objTb.find("input[name='dtRowID']");
        if (objRowID.length <= 0) {
            return false;
        }
        else {
            objRowID.each(function(i) {
                if (this.checked) {
                    //input > tr > td
                    $(this).parent().parent().remove();
                    updateRowNo(tableName);
                }
            });            
        }
    }
}
//--------------------------------------
// 更新行号
function updateRowNo(tableName) {
    var objTb = $('#' + tableName);
    var objRowID = objTb.find("input[name='dtRowID']");
    if (objRowID.length <= 0) {
        return false;
    }
    else {
        objRowID.each(function(i) {
            $(this).val(++i);
            $(this).parent().next('#dtRowNo').text(i);
        });
    }
}
//--------------------------------------


function InsertDefaultRows(tableName) {
    var value_array = new Array("01", "02", "03", "04", "05");
    var name_array = new Array("新建", "修改", "删除", "列表", "详细");

    var count = value_array.length;
    for (var i = 0; i < count; i++) {
        insertNewRow(tableName);
    }
    $('input[name="SF_NAME_ID"]').each(function(i) {
        $(this).val(value_array[i % count]);
    });
    $('input[name="SF_NAME"]').each(function(i) {
        $(this).val(name_array[i % count]);
    });
}
// if return to the tabs section
function CancelButton(retURL) {
    var tabID = "easywebTabsDiv";
    var obj = $('#' + tabID);
    if (obj.length > 0) {
        var tab = obj.tabs('getSelected');
        var id = $('#FN_ID', tab).val();
     
        $('#' + tabID).tabs('update', {
            tab: tab,
            options: {
                cache:false,
                href: retURL
            }
        })
    }
}
//--------------------------------------

//--------------------------------------
// ---- 载入页面所需的相关Script脚本 ----
//document.write("<script language=\"VBScript\" src=\"..\/js\/vbfunction.vbs\"><\/script>");
//document.write("<script language=\"JavaScript\" src=\"..\/js\/check.js\"><\/script>");
document.write("<script language=\"JavaScript\" src=\"..\/js\/CRM.js\"><\/script>");
//document.write("<script language=\"JavaScript\" src=\"..\/js\/keyevent.js\"><\/script>");
